package com.eascs.dubbo.api.workflow;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.workflow.InnerExamineApiService;
import com.eascs.admin.o2o.internal.entity.AuditComments;
import com.eascs.admin.o2o.internal.form.LoanapplyForm;
import com.eascs.admin.o2o.internal.service.AuditCommentService;
import com.eascs.admin.o2o.internal.service.PayMentService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loan.utils.BankRate;

@Service
public class InnerExamineApiServiceImpl implements InnerExamineApiService{
	
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	
	@Autowired
	private AuditCommentService auditCommentService;
	
	@Autowired
	private PayMentService payMentService;

	@Override
	public String saveInnerInfo(LoanapplyForm loanapplyForm) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(loanapplyForm.getLoanapplyNo());
		if (loanapplyForm.getRisk().doubleValue() < 0) {
			return "风险补偿金不能小于0";
		}
		if (loanapplyForm.getEaMount().compareTo(new BigDecimal(1000000.00)) == 1) {
			return "EA批复金额不能大于100万";
		}
		if (StatusContexts.LOAN_TYPE_BK.equals(loanapply.getLoanType()) || StatusContexts.LOAN_TYPE_BP.equals(loanapply.getLoanType()) || StatusContexts.LOAN_TYPE_BP_JX.equals(loanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SG.equals(loanapply.getLoanType()) || StatusContexts.LOAN_TYPE_YC.equals(loanapply.getLoanType()) || StatusContexts.LOAN_TYPE_SC.equals(loanapply.getLoanType())) {
			if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_BC)) {
				loanapplyForm.setLoanrate(BankRate.BANK_BC_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CBC)) {
				loanapplyForm.setLoanrate(BankRate.BANK_CBC_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_GACZ)) {
				loanapplyForm.setLoanrate(BankRate.BANK_GACZ_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CCBHN)) {
				loanapplyForm.setLoanrate(BankRate.BANK_CCBHN_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_QLB)) {
				loanapplyForm.setLoanrate(BankRate.BANK_QLB_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_CIBJN)) {
				loanapplyForm.setLoanrate(BankRate.BANK_CIBJN_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_JCB)) {
				loanapplyForm.setLoanrate(BankRate.BANK_JCB_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_ZZB)) {
				loanapplyForm.setLoanrate(BankRate.BANK_ZZB_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SPDBGZ)) {
				loanapplyForm.setLoanrate(BankRate.BANK_SPDBGZ_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SXB)) {
				loanapplyForm.setLoanrate(BankRate.BANK_SXB_RATE);
			} else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_ZLB)) {
				loanapplyForm.setLoanrate(BankRate.BANK_ZLB_RATE);
			}else if (loanapplyForm.getBankLoanName().equals(BankRate.BANK_SPDBJN)) {
				loanapplyForm.setLoanrate(BankRate.BANK_SPDBJN_RATE);
			} else {
				loanapplyForm.setLoanrate(BankRate.BANK_OTH_RATE);
			}
			if (!StringUtils.isNotEmpty(loanapplyForm.getRemark())) {
				List<AuditComments> auditComments = auditCommentService.findAuditsList(loanapplyForm.getLoanapplyNo());
				if (auditComments != null && !auditComments.isEmpty()) {
					AuditComments auditComment = auditComments.get(0);
					BigDecimal rate = new BigDecimal(100);
					if (loanapplyForm.getGeneralRate().compareTo(auditComment.getGeneralRate()) != 0) {
						return "上次综合费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(auditComment.getGeneralRate().multiply(rate), 4) + "%与本次修改综合费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(loanapplyForm.getGeneralRate().multiply(rate), 4) + "%,请填写备注";
					}
					if (loanapplyForm.getDepositrate().compareTo(auditComment.getDepositrate()) != 0) {
						return "上次保证金费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(auditComment.getDepositrate().multiply(rate), 3) + "%与本次保证金费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(loanapplyForm.getDepositrate().multiply(rate), 3) + "%,请填写备注";
					}
					if (loanapplyForm.getManagefeerate().compareTo(auditComment.getManagefeerate()) != 0) {
						return "上次管理费费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(auditComment.getManagefeerate().multiply(rate), 3) + "%与本次管理费费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(loanapplyForm.getManagefeerate().multiply(rate), 3) + "%,请填写备注";
					}
					if (loanapplyForm.getRisk().compareTo(auditComment.getRisk()) != 0) {
						return "上次风险补偿金费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(auditComment.getRisk().multiply(rate), 4) + "%与本次风险补偿金费率：" + com.eascs.admin.common.utils.StringUtils.bdFormat(loanapplyForm.getRisk().multiply(rate), 4) + "%,请填写备注";
					}
				}
			}
			payMentService.bKloan(loanapplyForm, loanapply);
		} 
		return null;
	}

}
